package cn.iocoder.yudao.module.security.service.risk;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.security.controller.admin.risk.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.risk.SecurityRiskDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.security.dal.mysql.risk.SecurityRiskMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.security.enums.ErrorCodeConstants.*;

/**
 * 风险分析 Service 实现类
 *
 * @author cai
 */
@Service
@Validated
public class SecurityRiskServiceImpl implements SecurityRiskService {

    @Resource
    private SecurityRiskMapper riskMapper;

    @Override
    public Long createRisk(SecurityRiskSaveReqVO createReqVO) {
        // 插入
        SecurityRiskDO risk = BeanUtils.toBean(createReqVO, SecurityRiskDO.class);
        riskMapper.insert(risk);
        // 返回
        return risk.getId();
    }

    @Override
    public void updateRisk(SecurityRiskSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskExists(updateReqVO.getId());
        // 更新
        SecurityRiskDO updateObj = BeanUtils.toBean(updateReqVO, SecurityRiskDO.class);
        riskMapper.updateById(updateObj);
    }

    @Override
    public void deleteRisk(Long id) {
        // 校验存在
        validateRiskExists(id);
        // 删除
        riskMapper.deleteById(id);
    }

    private void validateRiskExists(Long id) {
        if (riskMapper.selectById(id) == null) {
            throw exception(RISK_NOT_EXISTS);
        }
    }

    @Override
    public SecurityRiskDO getRisk(Long id) {
        return riskMapper.selectById(id);
    }

    @Override
    public PageResult<SecurityRiskDO> getRiskPage(SecurityRiskPageReqVO pageReqVO) {
        return riskMapper.selectPage(pageReqVO);
    }

}